package com.example.hongyu.avtt.ui.activity;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.hongyu.avtt.R;
import com.example.hongyu.avtt.config.Constant;
import com.example.hongyu.avtt.entity.TabEntity;
import com.example.hongyu.avtt.event.Event;
import com.example.hongyu.avtt.ui.adapter.FragmentViewPagerAdapter;
import com.example.hongyu.avtt.ui.base.BaseActivity;
import com.example.hongyu.avtt.ui.fragment.AliveFragment;
import com.example.hongyu.avtt.ui.fragment.HomeFragment;
import com.example.hongyu.avtt.ui.fragment.ImageFragment;
import com.example.hongyu.avtt.ui.fragment.TxtFragment;
import com.example.hongyu.avtt.ui.fragment.VideoFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

public class HomeActivity extends BaseActivity {
    @BindView(R.id.tab_layout)
    CommonTabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewPager;

    int index=0,pagenum,newPosition=-1;

    ArrayList<Fragment> fragments=new ArrayList<>();
    ArrayList<String> titles=new ArrayList<>();
    ArrayList<CustomTabEntity> tabEntities=new ArrayList<>();
    FragmentViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initActivityData() {
        HomeFragment homeFragment=new HomeFragment();
        VideoFragment videoFragment=new VideoFragment();
        TxtFragment txtFragment=new TxtFragment();
        ImageFragment imageFragment=new ImageFragment();
        AliveFragment aliveFragment=new AliveFragment();
        fragments.add(homeFragment);
        fragments.add(txtFragment);
        fragments.add(imageFragment);
        fragments.add(videoFragment);
        fragments.add(aliveFragment);


        adapter=new FragmentViewPagerAdapter(getSupportFragmentManager(),titles,fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setTabData(tabEntities);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(pagenum==position){
                    refreshPageData();
                }else {
                    pagenum=position;
                    tabLayout.setCurrentTab(position);
                    EventBus.getDefault().post(new Event<>(Constant.FRAGMENT_SELECT_CHANGE_EVENT, titles.get(position)));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {


                    tabLayout.setCurrentTab(position);
                    viewPager.setCurrentItem(position);
                    EventBus.getDefault().post(new Event<>(Constant.FRAGMENT_SELECT_CHANGE_EVENT, titles.get(position)));

            }

            @Override
            public void onTabReselect(int position) {
                refreshPageData();
            }
        });

        tabLayout.setCurrentTab(index);
    }

    private void refreshPageData() {
        EventBus.getDefault().post(new Event<>(Constant.REFRESH_PAGE_DATA_EVENT));
    }

    @Override
    protected void initContentView() {
        tabEntities.add(new TabEntity("首页",R.drawable.ic_baseline_home_24_on,R.drawable.ic_baseline_home_24));
        tabEntities.add(new TabEntity("文本",R.drawable.ic_baseline_text_snippet_24_on,R.drawable.ic_baseline_text_snippet_24));
        tabEntities.add(new TabEntity("图片",R.drawable.ic_baseline_image_24_on,R.drawable.ic_baseline_image_24));
        tabEntities.add(new TabEntity("视频",R.drawable.ic_baseline_videocam_24_on,R.drawable.ic_baseline_videocam_24));
        tabEntities.add(new TabEntity("直播",R.drawable.ic_baseline_live_tv_24_on,R.drawable.ic_baseline_live_tv_24));
        titles.add("首页");
        titles.add("文本");
        titles.add("图片");
        titles.add("视频");
        titles.add("直播");
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_home;
    }

    @Override
    protected void onEventProcess(Event event) {

    }
}